#include<iostream>
const int N = 1e6 + 10;
int ne[N], e[N], h, id;
using namespace std;
int main()
{
	int n; cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> ne[i];
		e[i] = i;
	}
	int h; cin >> h;
	ne[0] = h;
	for (int i = h; i != 0; i = ne[i])
	{
		cout << e[i] << ' ';
	}
	cout << endl;
	return 0;
}